/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOrder;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.OrderOnlineModel;
import com.ruipengkj.service.TOrderService;
import com.ruipengkj.view.base.BasePagePanel;


/**在线订单
 * @Author：pinea
 * @Date：2012-03-14
 */
public class OrderOnlinePanel extends BasePagePanel<TOrder> implements Runnable{

	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent orderNum; //预订单号
	private JLabelJComboBoxComponent orderStatus; //订单状态
	private JLabelJComboBoxComponent orderSource;//订单来源
	private DataPackageComponent startTime; //开始时间
	private DataPackageComponent endTime;	//结束时间
	
	private JLabelJTextFieldComponent usernameTF;//用户名
	private JLabelJTextFieldComponent mobileTF;//手机号码
	
	private JButton showButton; //查看详细
	
	private JTable table;
	private OrderOnlineModel orderOnlineModel;
	private JButton queryButton;//查询按钮
	private JButton clearButton;//清除按钮
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TOrder> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TOrderService tOrderService;
	
	public OrderOnlinePanel(){
		tOrderService=BuildSpringApplication.getInstance().getTOrderServiceImpl();
		this.setLayout(new BorderLayout());
		this.initView();
		this.initListener();
	}
	
	private void initView(){
		pageView=new PageView<TOrder>();
		orderOnlineModel=new OrderOnlineModel();
		table=uiFactory.createRPJTable(orderOnlineModel, null, this, 30, JLabel.CENTER);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		this.add(topPanel(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(tailPanel(),BorderLayout.SOUTH);
		//排序
		TableRowSorter<OrderOnlineModel> tableRowSorter = new TableRowSorter<OrderOnlineModel>(orderOnlineModel);  
		table.setRowSorter(tableRowSorter);
	}

	private JPanel tailPanel() {
		showButton= new RPJButtonRoundGray("查看详细");
		return new JPanelGridLayoutComponent(new Component[]{new JPanelFlowLayoutComponent(showButton, FlowLayout.LEFT),pagingComponent}, 1, 2);
	}
	
	private JPanel topPanel(){
		QueryPanel queryPanel=new QueryPanel("在线订单",ImageFactory.loadImage(ImageFactory.FUN_1_ICON_2));
		JPanel top_jp=uiFactory.createGridJPanel(2, 1);
		JPanel jp1=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		orderNum=new JLabelJTextFieldComponent("订单编号：", 15);
		usernameTF=new JLabelJTextFieldComponent("用户名：", 15);
		mobileTF=new JLabelJTextFieldComponent("手机号：", 15);
		orderStatus=new JLabelJComboBoxComponent("订单状态：",ApplicationContext.ORDER_STATUS_MAP(), 80);
		orderSource=new JLabelJComboBoxComponent("订单来源：",ApplicationContext.ORDER_SOURCE_MAP() ,80);
		jp1.add(orderNum);
		jp1.add(usernameTF);
		jp1.add(mobileTF);
		jp1.add(orderStatus);
		jp1.add(orderSource);
//		Util.setSize(jp1, 30, 30);
		
		JPanel jp2=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		startTime=new DataPackageComponent("预订时间：", 10);
		endTime=new DataPackageComponent("时间：", 10);
		queryButton=new RPJButtonBlue("查询");
		clearButton=new RPJButtonGray("清空");
		jp2.add(startTime);
		jp2.add(endTime);
		jp2.add(queryButton);
		jp2.add(clearButton);
//		Util.setSize(jp2, 30, 30);
		top_jp.add(jp1);
		top_jp.add(jp2);
		
		queryPanel.addPanel(top_jp);
//		queryPanel.addBorderPanel(top_jp);
		return queryPanel;
	}

	private void initListener(){
		queryButton.addActionListener(this);
		clearButton.addActionListener(this);
		showButton.addActionListener(this);
		orderNum.getText().addKeyListener(this);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2 && e.getSource() == table) {
			showButton.doClick();
		}
	}
	
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	@Override
	protected void termQuery(final int currentPage) {
		final TOrder tOrder=new TOrder();
		tOrder.setOrderID(orderNum.getText().getText().trim());
		tOrder.setHappenTime(startTime.getDate());
		tOrder.setEndTime(endTime.getDate());
		tOrder.setSource((Byte)orderSource.getKeyByVaule(orderSource.getBox().getSelectedItem().toString()));
		tOrder.setStatus((Byte)orderStatus.getKeyByVaule(orderStatus.getBox().getSelectedItem().toString()));
		TUser user=new TUser();
		user.setMobile(mobileTF.getText().getText().trim());
		user.setUserName(usernameTF.getText().getText().trim());
		tOrder.setTUser(user);
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TOrder> pageModel=tOrderService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tOrder);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						orderOnlineModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								pagingComponent.show();
								TabelUtil.setJTabelAutoClumnWidth(table);
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						orderOnlineModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TOrder> getPageView() {
		return pageView;
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		} else if(e.getSource()==showButton){
			//查看详细
			if (table.getSelectedRow() != -1) {
				String orderID = (String) table.getValueAt(table.getSelectedRow(), 0);
				OrderDetailDialog orderDetailDialog = new OrderDetailDialog(null, "销售详细", true);
				orderDetailDialog.showDialog(orderID);
			} else {
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==clearButton){
			//清除
			orderNum.getText().setText("");
			usernameTF.getText().setText("");
			mobileTF.getText().setText("");
			orderStatus.getBox().setSelectedIndex(0);
			orderSource.getBox().setSelectedIndex(0);
			startTime.setDate(null);
			endTime.setDate(null);
		}
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getSource()==orderNum.getText()){
			//查询
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				queryButton.doClick();	
			}
		}
	}

	@Override
	public void clear() {
		orderOnlineModel.clear();
		orderNum.getText().setText(""); //预订单号
		orderStatus.getBox().setSelectedIndex(0); //订单状态
		orderSource.getBox().setSelectedIndex(0);//订单来源
		startTime.setDate(null); //开始时间
		endTime.setDate(null);	//结束时间
		usernameTF.getText().setText("");//用户名
		mobileTF.getText().setText("");//手机号码
	}
}
